<?php
/**
 * Created by : Sandi Rizky Kharisma (sandi.informatics08@gmail.com)
 * This Class Generated for CRUD purpose only
 */
class barang extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->library('auth');
        $this->load->library('base');
        $this->load->library('ajaxresponse');
        $this->load->library('datatables');
        $this->load->model('crud/m_barang');
    }

    public function crud()
    {
        $this->auth->is_logged_in('form/login');
        $this->base->head_html('Tabel barang');
        $this->load->model("crud/m_tabel_satuan");
        $data['Satuan'] = $this->m_tabel_satuan->get();
        $this->load->model("crud/m_jenis_barang");
        $data['JenisBarang'] = $this->m_jenis_barang->get();
        $this->load->model("crud/m_toko");
        $data['Toko'] = $this->m_toko->get();
        $this->load->view('crud/barang', $data);
    }

    public function get_all($id_toko = null)
    {
        $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();
        $this->response->data = $this->m_barang->get($id_toko)->result();
        echo json_encode($this->response);
    }

    public function get_all_dialog($id_toko = null)
    {
        $this->datatables
           ->from('barang');

        echo $this->datatables->generate();
    }

    public function get($id, $id_toko = null)
    {
        $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();
        $this->response->data = $this->m_barang->get_one($id_toko, $id)->result_array();
        echo json_encode($this->response);
    }

    public function add($id_toko = null)
    {
        $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();
        $id_inserted = $this->m_barang->insert($id_toko);

        $this->load->model('crud/m_barang_toko');
        $this->response->data = $this->m_barang_toko->insert($id_toko, $id_inserted);
        echo json_encode($this->response);
    }

    public function update($id, $id_toko = null)
    {
        $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();
        $this->response->data = $this->m_barang->update($id_toko, $id);

        $this->load->model('crud/m_barang_toko');
        $this->response->data = $this->m_barang_toko->update($id_toko, $id);
        echo json_encode($this->response);
    }
    
    public function delete($id)
    {
        $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();
        $this->response->data = $this->m_barang->delete($id);
        echo json_encode($this->response);
    }
}
?>